import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  // 登录
  {
    path: '/',
    name: 'Login-register',
    component: () => import('../views/Login-register/index.vue'),
    redirect: '/login',
    meta: {
      title: '登录'
    },
    children: [
      {
        path: '/login',
        name: 'login',
        component: () => import('../views/Login-register/login.vue')
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('../views/Login-register/register.vue')
      }
    ]
  },
  // 首页
  {
    path: '/home',
    component: () => import('../Lyout/index.vue'),
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/Home/index.vue')
      }
    ]
  },
  // 问卷
  {
    path: '/paper',
    name: 'paper',
    component: () => import('../Lyout/index.vue'),
    redirect: '/paper/list',
    children: [
      {
        path: '/paper/add',
        name: 'paperAdd',
        component: () => import('../views/Paper/PaperAdd/index.vue')
      },
      {
        path: '/paper/list',
        name: 'paperList',
        component: () => import('../views/Paper/PaperList/index.vue')
      },
      {
        path: '/paper/edit/:id',
        name: 'paperEdit',
        component: () => import('../views/Paper/PaperAdit/index.vue')
      }
    ]
  },
  {
    path: '/process',
    name: 'process',
    component: () => import('../Lyout/index.vue'),
    redirect: '/process/list',
    children: [
      {
        path: '/process/list',
        name: 'processList',
        component: () => import('../views/Process/recruit-list/index.vue')
      },
      {
        path: '/process/control',
        name: 'processControl',
        component: () => import('../views/Process/process-list/index.vue')
      },
      {
        path: '/process/edit:id',
        name: 'processEdit',
        component: () => import('../views/Process/process-edit/index.vue')
      },
      {
        path: '/process/eventedit:id',
        name: 'eventEdit',
        component: () => import('../views/Process/process-list/eventEdit.vue')
      }
    ]
  },
  // 用户
  {
    path: '/users',
    name: 'user',
    component: () => import('../Lyout/index.vue'),
    meta: {
      title: '用户'
    },
    children: [
      {
        path: '/users/user',
        name: 'user',
        component: () => import('../views/User-admin/Wechat-list/index.vue')
      },
      {
        path: '/users/admin',
        name: 'admin',
        component: () => import('../views/User-admin/Admin-list/index.vue')
      },
      {
        path: '/users/paperList/:id',
        name: 'userPaperList',
        component: () => import('../views/User-admin/Paper-list/index.vue')
      },
      {
        path: '/users/paperDetail/:id/:paper_id',
        name: 'userPaperDetail',
        component: () => import('../views/User-admin/Paper-detail/index.vue')
      }
    ]
  },
  {
    path: '/interviewManagement',
    name: 'interviewManagement',
    component: () => import('../Lyout/index.vue'),
    meta: {
      title: '面试'
    },
    children: [
      {
        path: '/interviewManagement/interviewArrangement',
        name: 'interviewArrangement',
        component: () => import('../views/Interview/interview/InterviewList/index.vue')
      },
      {
        path: '/interviewManagement/sessionList:id',
        name: 'sessionList',
        component: () => import('../views/Interview/session/SessionList/index.vue')
      },
      {
        path: '/interviewManagement/list/:id/reserve',
        name: 'sessionReserveList',
        component: () => import('../views/Interview/session/SessionReserve/index.vue')
      },
      {
        path: '/interviewManagement/messageList',
        name: 'messageList',
        component: () => import('../views/Interview/messeage/index.vue')
      },
      {
        path: '/interviewManagement/messageUser',
        name: 'messageUser',
        component: () => import('../views/Interview/messeage/MessageUser.vue')
      },
      {
        path: '/interviewManagement/list/:id/sign',
        name: 'sessionSignList',
        component: () => import('../views/Interview/session/SessionSign/index.vue')
      }
    ]
  },
  //评分模块
  {
    path: '/score',
    name: 'score',
    component: () => import('../Lyout/index.vue'),
    meta: {
      title: '评分'
    },
    children: [
      {
        path: '/score/operate/:id',
        name: 'scoreOperate',
        component: () => import('../views/Score/ScoreOperate/index.vue')
      },
      {
        path: '/score/list:id',
        name: 'scoreList',
        component: () => import('../views/Score/ScoreList/index.vue')
      }
      // {
      //   path: "/score/total",
      //   name: "scoreTotal",
      //   component: ScoreTotal,
      // }
    ]
  }
]
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.name === 'login' || to.name === 'register') {
    //如果是前往登录和注册界面的就不需要判断token
    next()
  } else if (localStorage.getItem('token')) {
    //前往的如果是其他界面的话就判断token，且先判断token是否过期（五小时）
    const nowtime = new Date().getTime()
    const mistiming = nowtime - Number(localStorage.getItem('loginTime'))
    if (mistiming < 18000000) {
      //token未超过五小时就放行
      next()
    } else {
      //超过就清除localstorage，并前往login界面
      localStorage.clear()
      next({ name: 'login' })
    }
  } else {
    //没有token说明没有登录，前往login界面
    next({ name: 'login' })
  }
})

export default router
